#training

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException 

from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import ElementNotVisibleException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import Select
from selenium.webdriver import ActionChains


#user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
#headers={'User-Agent':user_agent,} 

def clear(element):
    value = element.get_attribute('value')
    if len(value) > 0:
        for char in value:
            element.send_keys(Keys.BACK_SPACE)
       
def init_driver():
    driver = webdriver.Firefox()
    driver.wait = WebDriverWait(driver, 5)
    return driver
 
def lookup(driver, query):
    driver.get("http://www.google.com")
    try:
        box = driver.wait.until(EC.presence_of_element_located(
            (By.NAME, "q")))
        button = driver.wait.until(EC.element_to_be_clickable(
            (By.NAME, "btnK")))
        box.send_keys(query)
        try:
            button.click()
        except ElementNotVisibleException:
            button = driver.wait.until(EC.visibility_of_element_located(
                (By.NAME, "btnG")))
            button.click()
    except TimeoutException:
        print("Box or Button not found in google.com")
    
#if __name__ == "__main__":
    #driver = init_driver()
    #lookup(driver, "Selenium")

url = "https://www.exploit-db.com/"
url = "https://forums.kali.org/"
driver = webdriver.Firefox()
driver.get(url)
driver.maximize_window()
#driver.find_element_by_xpath("//*[@class='tlid-input-button input-button header-button tlid-input-button-docs documents-icon']")
try:
    time.sleep(3)
    ele = driver.find_element_by_id("showFilters")
    print(ele.is_displayed())
    print(ele.is_enabled())
    
    #driver.wait.until(EC.element_to_be_clickable((By.ID, "showFilters"))).click()
    
    form = driver.find_element_by_partial_link_text('GET CERTIFIED') 
   # driver.find_element_by_class_name("form-control").click()
    #driver.find_element_by_class_name("form-control").send_keys("Meta") # symbols input
    #form = driver.find_element_by_id('verifiedCheck')
    form.click()
    #form.send_keys('Meta')
  
except NoSuchElementException as exception:
    print ("Element not found and test failed")
    
#time.sleep(10)
driver.implicitly_wait(10)

try:       
        box = WebDriverWait(driver,8).until(EC.presence_of_element_located((By.CLASS_NAME, "textbox")))
        button = WebDriverWait(driver,8).until(EC.element_to_be_clickable((By.CLASS_NAME, "searchbutton")))
        

        #dd = driver.find_element_by_class_name('form-check')
        #print(dd.is_enabled())
        
        #ii = Select(driver.find_element_by_partial_link_text("Penetration Testing with Kali Linux (PWK)"))
        #ii.select_by_visible_text('Wireless Attacks (WiFu)')
        #ii.click()
        #ii.setAttribute("value", "25")
        
        #box.click()   
        #box.setAttribute("value", "gg")
        try:
            button.click()
            
        except ElementNotVisibleException:
            button = WebDriverWait(driver,8).until(EC.visibility_of_element_located((By.CLASS_NAME, "searchbutton")))
            button.click()
            
            
        but1 = WebDriverWait(driver,8).until(EC.element_to_be_clickable((By.CLASS_NAME, "searchtype")))  
        try:
            but1.click()
            
        except ElementNotVisibleException:
            but1 = WebDriverWait(driver,8).until(EC.visibility_of_element_located((By.CLASS_NAME, "searchtype")))
            but1.click()
        
        name =  WebDriverWait(driver,8).until(EC.visibility_of_element_located((By.ID, "userfield_txt")))
        name.send_keys("john")
        
        but2 = WebDriverWait(driver,8).until(EC.visibility_of_element_located((By.ID, "tag_add_input")))
        but2.send_keys("reverse")
        
        enter = WebDriverWait(driver,8).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/form[2]/div/div[3]/div/input[1]")))
        enter.click()
        
        regi = WebDriverWait(driver,8).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/form[1]/ol/li/a[2]/b")))
        regi.click()
        
        driver.implicitly_wait(10)
        
        elem_month = driver.find_element_by_xpath('//*[@id="month"]')
        drp = Select(elem_month)
        drp.select_by_visible_text('August')
        
        elem_day = driver.find_element_by_xpath('/html/body/div[2]/form/div[1]/div/ul/li[2]/select/option[1]').context_click()
        #drp = Select(elem_day)
        #drp.select_by_visible_text('3')
        
        #int x = driver.findElement(By.xpath('//*[@id="month"]')).getLocation().getX();
        #get_element_position_top (self, location)
        #get_element_position_left (self, location) 
        
        
        actions = ActionChains(driver)
        actions.move_by_offset(100, 100).perform()
        time.sleep(5)
        actions.click().perform()
        
        actions.context_click().perform()
        
        #drp.select_by_index(3)
        #el2 = driver.find_element_by_id('//*[@id="year"]')
        #drp2 = Select(el2)
        
        
except TimeoutException:
    print("Box or Button not found")